<template>
  <!-- 小模块公共组件 -->
  <div class="Mould-Box">
    <!-- 图表标题 -->
    <div class="Mould-Box-mintitle">
      <div class="charttitle">
        {{ chartTitle }}
      </div>
      <div class="line" />
      <div />
    </div>
    <!-- 图表 -->
    <div class="Mould-Box-chartBox">
      <div class="chart-wrap">
        <div class="box-left">
          <div class="pie-chart">
            <PieChart
              :data="data1"
              :color="color"
              text="各时段用电量"
              :src="require('@/assets/img/electric/icon-1.png')"
            />
          </div>
        </div>
        <div class="legend">
          <Legend1
            v-for="(item, index) in data1"
            :key="item.x"
            class="item"
            :name="item.x"
            :num="item.y"
            :color="color[index]"
            unit="MW·h"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import PieChart from '@/components/chart/pie/PieChart.vue';
import Legend1 from '@/components/chart/legend/Legend1.vue';

export default {
  name: 'MouldBobV2',

  components: {
    PieChart,
    Legend1
  },
  props: {
    datas: {
      type: Array,
      default: () => []
    },
    orderKeys: {
      type: Array,
      default: () => []
    },
    color: {
      type: Array,
      default: () => ['#34C8FF', '#FB9B2A', '#57FFAA', '#FFD101']
    },
    chartTitle: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
    };
  },
  computed: {
    data1() {
      return this.formatChartData(this.orderKeys, this.datas);
    }
  },
  methods: {
    // 调整图表数据格式，以及数据元素的排列顺序
    formatChartData(orderKeys, origin) {
      return orderKeys.map((orderKey) => {
        for (const item of origin) {
          if (orderKey === item.level1Desc) {
            return {
              x: item.level1Desc,
              y: item.alarmCount
            };
          }
        }
        return {
          x: orderKey,
          y: 0
        };
      });
    }
  }
};
</script>

<style lang='scss' scoped>
.Mould-Box {
  width: 20.625rem;
  &-mintitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: .9375rem;
    .charttitle {
      font-size: 0.75rem;
      color: $light2Grey;
      margin-right: 0.3125rem;
    }
    .line {
      flex: 1;
      height: 0.0625rem;
      border-bottom: 0.0625rem dashed $darkGrey;
    }
  }
  &-chartBox {
    width: 100%;
    height: 7.5rem;
    .chart-wrap{
      display: flex;
      height: 7.5rem;
      .box-left{
        width: 10rem;
        height: 7.5rem;
        .pie-chart{
          width: 7.5rem;
          height: 7.5rem;
          margin: 0 auto;
        }
      }
      .legend{
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: center;
        padding-right: 1.4375rem;
        box-sizing: border-box;
        .item{
          width: 3.75rem;
        }
      }
    }
  }
}
</style>
